package tech.yunqian.slim.framework.web;

import com.alibaba.fastjson.JSONObject;

import lombok.Data;

@Data
public class Pagination {
    private Integer pageNumber;
    private Integer pageSize;

    public static Pagination parse(JSONObject request) {
        Pagination pagination = new Pagination();
        Integer pageNumber = request.getInteger("pageNumber");
        Integer pageSize = request.getInteger("pageSize");
        if (pageNumber == null || pageSize == null) {
            return pagination;
        }
        if (pageNumber < 0) {
            pageNumber = 0;
        }
        pagination.setPageNumber(pageNumber);
        if (pageSize < 1) {
            pageSize = 10;
        }
        pagination.setPageSize(pageSize);
        return pagination;
    }

    public Integer getStart() {
        Integer start = null;
        if (pageNumber != null && pageSize != null) {
            start = pageNumber * pageSize;
        }
        return start;
    }

    public Integer getNumber() {
        Integer number = null;
        if (pageNumber != null && pageSize != null) {
            number = pageSize;
        }
        return number;
    }
}
